Working with the add command
Let's work with the add command, for example, for parsing your arguments and
calling the functions. Once the add command gets called, we want to call a
function defined in notes, which will be responsible for actually adding the note.
The notes.addNote function will get the job done. Now, what do we want to pass to
the addNote function? We want to pass in two things: the title, which is accessible
on argv.title, as we saw in the preceding example; and the body, argv.body:
console.log('Starting app.js');
const fs = require('fs');
const _ = require('lodash');
const yargs = require('yargs');
const notes = require('./notes.js');
const argv = yargs.argv;
var command = process.argv[2];
console.log('Command:', command);
console.log('Process', process.argv);
console.log('Yargs', argv);
if (command === 'add') {
console.log('Adding new note');
notes.addNote(argv.title, argv.body);
} else if (command === 'list') {
console.log('Listing all notes');
} else if (command === 'read') {
console.log('Reading note');
} else if (command === 'remove') {
console.log('Removing note');
} else {
console.log('Command not recognized');
}
Currently, these command-line arguments, title and body, aren't
required. So technically, the user could run the application without
one of them, which would cause it to crash, but in future, we'll be
requiring both of these.
Now that we have notes.addNote in place, we can remove our console.log statement,
which was just a placeholder, and we can move into the notes application
notes.js.
Inside notes.js, we'll get started by making a variable with the same name as the
method we used over app.js and addNote, and we will set it equal to an anonymous